package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.game.adventure.AdventureType;
import com.xcity.game.adventure.AdventureUtils;
import com.xcity.game.adventure.model.ActionTarget;
import com.xcity.game.adventure.model.Adventure;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.player.cache.PlayerCacheService;

import naga.x.App;
import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class AdventurePlayResponse extends Response {

	public int pos; // 当前位置
	public AdventureType type; // 奖励类型
	public List<Target> stealTargets;
	public Target attackTarget;
	public int exp = GameStaticConfig.adventureExp;
	
	public static class Target {
		public long id;
		public byte sex;
		public String name;
		public String icon;
		public int level;
		public long gold;
		public Target(ActionTarget ref) {
			Player player = App.getApp().getServiceManager().get(PlayerCacheService.class).find(ref.getId());
			this.id = player.getId();
			this.sex = player.getSex();
			this.name = player.getName();
			this.icon = player.getIcon();
			this.level = player.getLevel();
			this.gold = ref.getGold();
		}
	}
	
	public AdventurePlayResponse() {
		super(CODE_ERROR);
	}
	
	public AdventurePlayResponse(int serial, int pos, AdventureType type) {
		this(serial, pos, type, null, null);
	}
	
	public AdventurePlayResponse(int serial, int pos, List<ActionTarget> stealTargets) {
		this(serial, pos, AdventureType.STEAL, stealTargets, null);
	}
	
	public AdventurePlayResponse(int serial, int pos, ActionTarget attackTarget) {
		this(serial, pos, AdventureType.ATTACK, null, attackTarget);
	}
	
	public AdventurePlayResponse(int serial, int pos, AdventureType type, List<ActionTarget> stealTargets, ActionTarget attackTarget) {
		super(serial);
		this.pos = pos;
		this.type = type;
		this.stealTargets = wrapStealTargets(type, stealTargets);
		this.attackTarget = wrapAttackTarget(type, attackTarget);
	}
	
	public AdventurePlayResponse(int serial, int pos, Adventure adventure) {
		super(serial);
		this.pos = pos;
		this.type = adventure.getLastAction();
		this.stealTargets = wrapStealTargets(this.type, adventure.getLastActionTargets());
		this.attackTarget = wrapAttackTarget(this.type, adventure.getLastActionTargets());
	}
	
	private static List<Target> wrapStealTargets(AdventureType type, List<ActionTarget> list) {
		if (type != AdventureType.STEAL || list == null) {
			return null;
		}
		List<Target> result = new ArrayList<Target>(list.size());
		for (int i = 0; i < list.size(); i++) {
			result.add(new Target(list.get(i)));
		}
		return result;
	}
	
	private static Target wrapAttackTarget(AdventureType type, List<ActionTarget> targets) {
		return wrapAttackTarget(type, targets != null && targets.size() > 0 ? targets.get(0) : null);
	}
	
	private static Target wrapAttackTarget(AdventureType type, ActionTarget target) {
		if ((type != AdventureType.ATTACK && type != AdventureType.CAPTURE) || target == null) {
			return null;
		}
		Target result = new Target(target);
		result.id = AdventureUtils.fakeAttackTargetId(type, target.getId());
		return result;
	}
	
}
